package controllers;

import play.*;
import play.libs.Images;
import play.mvc.*;

import java.util.*;

import org.apache.ibatis.session.SqlSession;

import models.*;
import models.domain.Article;
import models.domain.User;
import models.mapper.ArticleMapper;
import models.mapper.UserMapper;
import mybatisplay.IbatisSessionFactory;

public class Application extends Controller {

    public static void index() {
        render();
    }
    
    public static void list() {
    	SqlSession session = IbatisSessionFactory.get().openSession();
    	ArticleMapper mapper = session.getMapper(ArticleMapper.class);
    	List<Article> arts = mapper.selectAll();
    	render(arts);
    }
    
    public static void login() {
    	render();
    }
    
    public static void loginCheck(String name,String password) {
    	SqlSession session = IbatisSessionFactory.get().openSession();
	    
	    UserMapper mapper = session.getMapper(UserMapper.class);
	    
	    User user = mapper.checkUser(name,password);
	    if(user != null) {
	    	render("Application/index.html",name);
	    }else {
	    	render("Application/list.html");
	    }
    }
    
    public static void add() {
    	render();
    }
    
    public static void captcha() {
    	Images.Captcha captcha = Images.captcha();
    	renderBinary(captcha);
    }

}